package net.shakewarner.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.shakewarner.PMF;
import net.shakewarner.dao.MeasurementDao;
import net.shakewarner.entity.Measurement;

public class MeasurementDaoImpl implements MeasurementDao, Serializable {

	private static final long serialVersionUID = -5231106593194708461L;

	@Override
	public Collection<Measurement> list() {
		Collection<Measurement> res = Collections.emptyList();

		final PersistenceManager pm = PMF.get().getPersistenceManager();

		final Query query = pm.newQuery(Measurement.class);

		try {

			// query.setFilter("deleteDate == null");

			@SuppressWarnings("unchecked")
			final Collection<Measurement> results = (Collection<Measurement>) query
					.execute();

			final Collection<Measurement> collection = pm
					.detachCopyAll(results);
			res = collection;
		} finally {
			pm.close();
			query.closeAll();
		}

		return res;
	}

	@Override
	public void saveAll(final Collection<Measurement> coll) {
		final PersistenceManager pm = PMF.get().getPersistenceManager();

		try {

			pm.makePersistentAll(coll);
		} finally {
			pm.close();
		}
	}

}
